<template>
  <el-breadcrumb class="app-breadcrumb" separator="/">
    <transition-group name="breadcrumb">
      <el-breadcrumb-item v-for="(item,index)  in levelList" :key="item.path" v-if='item.meta.title' v-show='!($route.path === "/projectList")'>
        <span v-if='item.redirect==="noredirect"||index==levelList.length-1' class="no-redirect">{{generateTitle(item.meta.title) === ('首页' || 'dashboard')?index_Title:generateTitle(item.meta.title)}}</span>
        <!-- <router-link style='color: #fff;' v-else :to="item.redirect||item.path">{{generateTitle(item.meta.title) === ('首页' || 'dashboard')?index_Title:generateTitle(item.meta.title)}}</router-link> -->
        <span
          v-else
          style='color: #333;cursor:pointer'
          @click='to(item.redirect||item.path)'>
            {{generateTitle(item.meta.title) === '首页'?index_Title:generateTitle(item.meta.title)}}
        </span>
      </el-breadcrumb-item>
    </transition-group>
  </el-breadcrumb>
</template>

<script>
import { generateTitle } from '@/utils/i18n'

export default {
  created() {
    this.getBreadcrumb()
  },
  data() {
    return {
      levelList: null,
      index_Title: ''
    }
  },
  computed: {
    factorys() {
      return this.$store.state.factory.factoryInfo || []
    }
  },
  watch: {
    $route(to, from) {
      // if (to.path === '/dashboard') {
      //   window.location.href = window.env.href
      //   return
      // }
      this.getBreadcrumb()
    },
    factorys(val, oldVal) {
      this.index_Title = this.getFactoryName()
    }
  },
  methods: {
    generateTitle,
    getBreadcrumb() {
      this.index_Title = this.getFactoryName()
      let matched = this.$route.matched.filter(item => item.name)
      const first = matched[0]
      if (first && first.name !== 'dashboard') {
        matched = [{ path: '/dashboard', meta: { title: 'dashboard' } }].concat(
          matched
        )
      }
      this.levelList = matched
    },
    getFactoryName() {
      return this.factorys.factoryName
    },
    to(path) {
      if (path === '/dashboard') {
        window.location.href = window.env.factoryHref + '/dashboard'
        return
      }
      this.getBreadcrumb()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.app-breadcrumb.el-breadcrumb {
  position: relative;
  top: 4px;
  display: inline-block;
  font-size: 15px;
  margin-left: 40px;
  .no-redirect {
    color: #477FE3;
    cursor: text;
  }
}
</style>
